var indexData = require('../../data/indexData.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight, //导航栏高度
    navTop: app.globalData.navTop, //导航栏距顶部距离
    navObj: app.globalData.navObj, //胶囊的高度
    navObjWid: app.globalData.navObjWid, //胶囊宽度+距右距离
    storeList: indexData.storeList,
    /**导航栏 */
    isRefresh: false,
    currentTab: 0,
    /**自定义导航栏列表 */
    tabList: indexData.tabList,
    /**默认第一个导航栏 */
    tabId: indexData.tabList[0].id,
    /**商品分类导航 */
    tabGoodsList: null,
    /**品牌导航 */
    tabBrandList: null,
    animationZ: null, //正面动画
    animationF: null, //反面动画
    pointId: 1, // 正反面动画锚点，默认为1
    inter: '',
    Interval: null, // 定时器声明时赋值
    Numbers: 0,
  },
  bindPickerChange: function (e) {
    this.setData({
      store_name: this.data.storeList[e.detail.value].name,
      store_name_sub: this.data.storeList[0].nameSub,
    })
  },
  onLoad: function (options) {
    var defaultTab = this.data.tabList[0];
    this.setData({
      store_name: this.data.storeList[0].name,
      store_name_sub: this.data.storeList[0].nameSub,
      tabName: defaultTab.name
    })
     //加载商品分类导航
     this.goodsListShow(defaultTab.id);
     //加载品牌分类导航
    this.brandListShow(defaultTab.id);
    //每3秒钟翻转一次品牌分类
    this.brandListTime(this.data.pointId); 
  },
  goodsListShow: function (e) {
    var tabGoodsList = indexData.tabGoodsList[e];
    console.log("classify="+e);
    this.setData({
      tabGoodsList: tabGoodsList
    })
  },
  brandListShow: function (e) {
    var tabBrandList = indexData.tabBrandList[e];
    console.log("brand="+e);
    this.setData({
      tabBrandList: tabBrandList
    })
  },
  // 导航切换监听
  tabNav(e) {
    let currentTab = e.currentTarget.dataset.index;
    let tabId = this.data.tabList[currentTab].id;
    this.setData({
      currentTab,
      tabName: this.data.tabList[currentTab].name,
      tabId: tabId
    })
    //加载商品分类导航
    this.classifyListShow(tabId);
    //加载品牌分类导航
    this.brandListShow(tabId);
  },
  handleSwiper(e) {
    let {current,source} = e.detail;
    if (source === 'autoplay' || source === 'touch') {
      const currentTab = current;
      let tabId = this.data.tabList[currentTab].id;

      this.setData({
        currentTab,
        tabName: this.data.tabList[currentTab].name,
        tabId: tabId
      })
       //加载商品分类导航
    this.classifyListShow(tabId);
    //加载品牌分类导航
    this.brandListShow(tabId);
    }
  },
  rotateFn(pointId) {
    let that = this
    // let id = e;
    // 点击正面
    if (pointId == 1) {
      this.setData({
        animationZ: that.animation.rotateY(180).step().export(),
        animationF: that.animation.rotateY(0).step().export(),
        pointId: 2
      })
    } else { //点击反面
      this.setData({
        animationZ: that.animation.rotateY(0).step().export(),
        animationF: that.animation.rotateY(180).step().export(),
        pointId: 1
      })
    }
  },
  brandListTime : function() {
    this.data.Interval = setInterval(() => {
      var pointId = this.data.pointId;
      console.log("pointId="+pointId);
      this.rotateFn(pointId);
      this.setData({
        Numbers: ++this.data.Numbers
      })
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  animation: wx.createAnimation({
    duration: 1000,
    timingFunction: 'linear'
  }),
  handleTolower(e){
    wx.showToast({
      title: '到底啦'
    })
  },
  refresherpulling() {
    wx.showLoading({
      title: '刷新中'
    })
    setTimeout(() => {
      this.setData({
        isRefresh: false
      })
      wx.showToast({
        title: '加载完成'
      })
    }, 1500)
  },


  onShow: function () {
    console.log("onShow......");
    let that = this
    clearInterval(that.data.Interval);
    that.brandListTime(); 
  },


  onReachBottom: function () {

  },
/**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("onHide......");
    let that = this
    clearInterval(that.data.Interval);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("onUnload......");
    let that = this
    clearInterval(that.data.Interval);
  },


})